home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.640 < prev    next >
Text File  |  1992-02-06  |  3KB  |  44 lines

  1. {\rtf0\ansi{\fonttbl\f1\fnil Times-Roman;\f0\fmodern Courier;}
  2. \paperw13040
  3. \paperh10800
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f1\b0\i0\ul0\fs28 alloc &  init vs. new\
  8. \
  9. Q:  When should I be using the alloc & init method of creating and initializing an object?  When should I be using +new?\
  10. \
  11. A: Since 2.0, you should be using alloc & init to create and initialize objects.  You use alloc & init in the following way:\
  12.     
  13. \f0\fs24 [[Class alloc] init]
  14. \f1\fs28 \
  15. Where 
  16. \b Class
  17. \b0  is the name of the class that you want to instantiate.  -init could be any method name which begins with the four characters "-init", such as "-initFrame" or "-initIt".  This allows you to cleanly separate the creation and the initialization phases of object instantiation.  It also helps the readibility of source code.  You can be sure now when looking at someone else's code, that -init methods are used to initialize an object that has just been created.  Finally, this allows us to use loadable pallettes within IB (or at least allows the appkit to initialize custom objects), because each object's initialization routine has a predictable name.\
  18. \
  19. In the 1.0 way of doing things an object was instantiated by calling +new, and initialization of that object was done in a method named anything you like.\
  20. \
  21. The change has some porting issues that you may have to contend with.\
  22. \
  23. Now suppose, that in your 1.0 custom class that you used +new to create an object and had a method, -init or -initXXX, which performed some set up operations.  Suppose also that you were calling this routine from somewhere else in your program but not at creation time.  In 1.0 that code would compile, link and run just fine.\
  24. \
  25. If you then try to port that code to 2.0 code you 
  26. \b may
  27. \b0  have some troubles.  
  28. \fc0 The kit instantiates custom objects when they are contained within a NIB file.  When instantiating an object the appkit determines whether to call +new or alloc & init.  Since you don't have a +new method but do have an -init method,  it will use [[Class alloc] init], thinking that you are a "new-style" object.  Here, your init method will be improperly called in 2.0 because the kit thinks it is something else.\
  29.  
  30. \pard\tx620\tx1240\tx1860\tx2480\tx3100\tx3720\tx4340\tx4980\tx5600\tx6220\fc0 \
  31. So, to create a new object under 2.0, you should be using the alloc & init method to create and initialize it.  Also, you should only name methods -init or -initXXX if you want them be called in the alloc & init method of creation.  (Actually, -initXXX will work if a method with that name hasn't already been declared by the Application Kit, but it's bad style to use such a name, since it will confuse people reading your code.)\
  32.  
  33. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600 \
  34. All of the examples in /NextDevelopers/Examples have been updated to reflect this change.\
  35. \
  36. QA640\
  37.  
  38. \fc0 \
  39.  
  40. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc0 Valid for 2.0
  41. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc0 \
  42. \
  43.  
  44.